//
//  DawnNavView.swift
//  DawnFoundation
//
//  Created by dawn on 2021/8/29.
//

import UIKit
import YYText

enum DawnButtonAlignment {
    case leftImg
    case rightImg
    case topImg
    case bottomImg
}
typealias DawnNavItem = (icon: String?, title: String?, color: UIColor?, (()->())?)
class DawnNavView: UIView {
    private let leftItemTag = 777
    private let rightItemTag = 888
    private var lastLeftItemTag = 777
    private var lastRightItemTag = 888
    private var contentBounds: CGRect!
    private var contentCenter: CGPoint!
    var leftItems: [(UIButton, CGFloat)]!
    {
        didSet{
            setupLeftItems(leftItems)
        }
    }
    var rightItems: [(UIButton, CGFloat)]!
    {
        didSet{
            setupRightItems(rightItems)
        }
    }
    
    var hasBottomLine = true {
        didSet{
            bottomLine.isHidden = !hasBottomLine
        }
    }
    private lazy var disposeBag = DisposeBag()
    init() {
        super.init(frame: .zero)
        frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.navH)
        contentBounds = CGRect(x: 0, y: 0, width: frame.width, height: frame.height-UIScreen.safeTop)
        contentCenter = CGPoint(x: frame.width/2, y: contentBounds.height/2 + UIScreen.safeTop)
        
        bottomLine = CALayer()
        bottomLine.backgroundColor = UIColor.hex(0xdcdcdc).cgColor
        bottomLine.frame = CGRect(x: 0, y: UIScreen.navH-0.5.adapter, width: UIScreen.width, height: 0.5.adapter)
//        layer.addSublayer(bottomLine)
        
        leftItems = [(UIButton, CGFloat)]()
        rightItems = [(UIButton, CGFloat)]()
    }
    
    @discardableResult
    func addTitle(_ v: UIView, isCenter: Bool = true) -> UIView {
        addSubview(v)
        v.top(UIScreen.safeTop + (contentBounds.height-v.bounds.height)/2)
        if isCenter {
            v.centerX(bounds.width/2)
        }else {
            v.left(viewWithTag(lastLeftItemTag-1)?.frame.maxX ?? 0).right((viewWithTag(lastRightItemTag-1)?.frame.minX ?? (frame.width)))
        }
        return v
    }

    @discardableResult
    func addLeft(icon: String? = nil,
                 title: String? = nil,
                 color: UIColor = .hex(0x333333),
                 width: CGFloat = 80.adapter,
                 font: UIFont = .iconfont(16),
                 style: UIButtonContentStyle = .none,
                 closure: (()->())?) -> UIButton {
        let btn = viewWithTag(lastLeftItemTag) as? UIButton ?? UIButton(type: .custom)
        btn.tag = lastLeftItemTag
        setupItem(btn: btn, icon: icon, title: title, color: color, font: font, style: style, closure: closure)
        btn.frame = CGRect(x: viewWithTag(lastLeftItemTag-1)?.frame.maxX ?? 0,
                           y: UIScreen.safeTop, width: width, height: contentBounds.height)
        lastLeftItemTag += 1
        return btn
    }
    
    @discardableResult
    func addRight(icon: String? = nil,
                 title: String? = nil,
                 color: UIColor = .hex(0x333333),
                 width: CGFloat = 80.adapter,
                 font: UIFont = .iconfont(16),
                 style: UIButtonContentStyle = .none,
                 closure: (()->())?) -> UIButton {
        let btn = viewWithTag(lastRightItemTag) as? UIButton ?? UIButton(type: .custom)
        btn.tag = lastRightItemTag
        setupItem(btn: btn, icon: icon, title: title, color: color, font: font, style: style, closure: closure)
        btn.frame = CGRect(x: (viewWithTag(lastRightItemTag-1)?.frame.minX ?? (frame.width)) - width,
                              y: UIScreen.safeTop, width: width, height: contentBounds.height)
        lastRightItemTag += 1
        return btn
    }
    
    private func setupItem(btn: UIButton,
                           icon: String? = nil,
                           title: String? = nil,
                           color: UIColor = .hex(0x333333),
                           font: UIFont = .iconfont(16),
                           style: UIButtonContentStyle = .none,
                           closure: (()->())?) {
        btn.da.set(style: style, space: 5.adapter)
        if btn.superview == nil {
            addSubview(btn)
        }
        if let title = title {
            btn.setTitle(title, for: .normal)
            btn.setTitleColor(color, for: .normal)
            btn.titleLabel?.font = font
        }
        if let icon = icon {
            btn.setImage(UIImage(named: icon), for: .normal)
        }
        btn.rx.tap.subscribe(onNext: {
            closure?()
        }).disposed(by: disposeBag)
    }
    
    private func setupLeftItems(_ items: [(UIButton, CGFloat)]) {
        items.forEach { item in
            if item.0.superview == nil {
                addSubview(item.0)
            }
            item.0.frame = CGRect(x: viewWithTag(lastLeftItemTag-1)?.frame.maxX ?? UIScreen.margin,
                                  y: UIScreen.safeTop, width: item.1, height: contentBounds.width)
            lastLeftItemTag += 1
        }
    
    }
    
    private func setupRightItems(_ items: [(UIButton, CGFloat)]) {
        items.forEach { item in
            if item.0.superview == nil {
                addSubview(item.0)
            }
            item.0.frame = CGRect(x: (viewWithTag(lastRightItemTag-1)?.frame.minX ?? (frame.width - UIScreen.margin)) - item.1,
                                  y: UIScreen.safeTop, width: item.1, height: contentBounds.width)
            lastRightItemTag += 1
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var titleV: UILabel = {
        let lbl = UILabel()
        lbl.font = .font(17)
        lbl.textColor = .hex(0x333333)
        lbl.textAlignment = .center
        lbl.center = contentCenter
        lbl.bounds = contentBounds
        addSubview(lbl)
        return lbl
    }()
    
    private var bottomLine: CALayer!
}
